
module sqlite3d.pub;

public import
	tango.stdc.stringz,
	sqlite3d.c.sqlite3,
	tango.core.Traits;

debug
	public import	tango.io.Stdout;
		
alias void delegate(Object) DisposeEvt;
public static const uint[]	Pow2a	= cast(uint[]) ctfe2_Pow2!(uint)(31);

/// compile time integer to string
char [] ctfe2_i2a(int i){
    char[] digit="0123456789";
    char[] res="".dup;
    if (i==0){
        return "0".dup;
    }
    bool neg=false;
    if (i<0){
        neg=true;
        i=-i;
    }
    while (i>0) {
        res=digit[i%10]~res;
        i/=10;
    }
    if (neg)
        return '-'~res;
    else
        return res;
}
/// ditto
char [] ctfe2_i2a(long i){
    char[] digit="0123456789";
    char[] res="".dup;
    if (i==0){
        return "0".dup;
    }
    bool neg=false;
    if (i<0){
        neg=true;
        i=-i;
    }
    while (i>0) {
        res=digit[cast(size_t)(i%10)]~res;
        i/=10;
    }
    if (neg)
        return '-'~res;
    else
        return res;
}
/// ditto
char [] ctfe2_i2a(uint i){
    char[] digit="0123456789";
    char[] res="".dup;
    if (i==0){
        return "0".dup;
    }
    bool neg=false;
    while (i>0) {
        res=digit[i%10]~res;
        i/=10;
    }
    return res;
}
/// ditto
char [] ctfe2_i2a(ulong i){
    char[] digit="0123456789";
    char[] res="".dup;
    if (i==0){
        return "0".dup;
    }
    bool neg=false;
    while (i>0) {
        res=digit[cast(size_t)(i%10)]~res;
        i/=10;
    }
    return res;
}

char[] ctfe2_lower(char[] s){
	char[] res	;
	foreach(int i, c;s){
		if( c >= 'A' && c <='Z' ){
			res	~= c + 32 ;
		}else{
			res	~= c;
		}
	}
	return res;
}
char ctfe2_upper(char c){
    if( 'a' <= c && c <= 'z' )
        return cast(char)((c - 'a') + 'A');
    else
        return c;
}
bool ctfe2_isSpace(T)(T c)
{
    static if (T.sizeof is 1)
        return (c <= 32 && (c is ' ' | c is '\t' | c is '\r'
                    | c is '\n' | c is '\v' | c is '\f'));
    else
        return (c <= 32 && (c is ' ' | c is '\t' | c is '\r'
                    | c is '\n' | c is '\v' | c is '\f'))
            || (c is '\u2028' | c is '\u2029');
}

int ctfe2_CountQuestionMark(char[] s){
	int n	= 0;
	foreach(c; s){
		if( c is '?' ){
			n++;
		}
	}
	return n;
}

public T[] ctfe2_Pow2(T)(byte len){
	T[] ary;
	for(byte i =0 ;  i < len; i++ ){
		T v	= 1;
		for(byte j ; j < i ; j++) {
			v	= v * 2;
		}
		ary	~= v;
	}
	return ary;
}

char[] ctfe2_remove_right(char[] s, char[] c){
	int i	= ctfe2_ifind(s, c);
	return s[0..i].dup;
}

int ctfe2_find(char[] b, char[] a){
	foreach(int j, c; b){
		if( b.length - j >= a.length ){
			if( b[j..j+a.length] == a ){
				return j;
			}
		}else{
			break;
		}
	}
	return b.length;
}
int ctfe2_ifind(char[] _b, char[] _a){
	char[] b	= ctfe2_lower(_b);
	char[] a	= ctfe2_lower(_a);
	return ctfe2_find(b, a);
}
int ctfe2_rfind(char[] _b, char[] _a){
	char[] b	= ctfe2_lower(_b);
	char[] a	= ctfe2_lower(_a);
	for(int i	= _b.length ; i-- > 0 ;){
		if( i >= a.length ){
			if( _b[ i - a.length .. i ]  == a ){
				return i - a.length;
			}
		}else{
			break;
		}
	}
	return b.length;
}
int ctfe2_irfind(char[] _b, char[] _a){
	char[] b	= ctfe2_lower(_b);
	char[] a	= ctfe2_lower(_a);
	return ctfe2_rfind(b ,a);
}
int ctfe2_find(char[] b, char a){
	foreach(int j, c; b){
		if( c == a ){
			return j;
		}
	}
	return b.length;
}
char[] ctfe2_ltrim(char[] b){
	char[] r	= b;
	foreach(int j, c; b){
		if( c == ' ' ){
			r	= b[ j .. $ ];
		}else{
			break;
		}
	}
	return r.dup;
}